// 封装 fetch
class CommonFetch {
  // get
  get(url) {
    return new Promise((resolve, reject) => {
      fetch(url, {
        method: 'get',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      })
        .then((ret) => {
          return ret.json()
        })
        .then((ret) => {
          resolve(ret)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
  // post,传的是JSON格式的参数
  JSONpost(url, JSONbody) {
    fetch(url, {
      method: 'post',
      body: JSONbody,
      headers: {
        'Content-Type': 'application/json',
      },
    })
      .then((ret) => ret.json())
      .then((ret) => {
        console.log(ret)
      })
  }
  // post,传递的是string格式的参数
  STRINGpost_json(url, STRINGbody) {
    return new Promise((resolve, reject) => {
      fetch(url, {
        method: 'post',
        body: STRINGbody,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      })
        .then((ret) => ret.json())
        .then((ret) => {
          resolve(ret)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
  STRINGpost_text(url, STRINGbody) {
    return new Promise((resolve, reject) => {
      fetch(url, {
        method: 'post',
        body: STRINGbody,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      })
        .then((ret) => ret.text())
        .then((ret) => {
          resolve(ret)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}
export default new CommonFetch()
